package com.ebs.spring.jms;

/**
 * Revision Author:
 *  $File: //depot/MarketData/fxmonitor/main/src/main/java/com/ebs/spring/jms/CachingConnectionFactoryAuth.java $
 *  $Revision: #1 $
 *  $DateTime: 2012/05/24 13:29:35 $
 *  $Change: 111268 $
 *  $Author: Arik.Sher $
 */

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;

import org.springframework.jms.connection.CachingConnectionFactory;
import org.springframework.util.Assert;

/**
 * extending Spring's class CachingConnectionFactory.
 * so could support method createConnection with username n password.
 * 
 * @see CachingConnectionFactory
 * @see ConnectionFactory
 */

public class CachingConnectionFactoryAuth
extends      CachingConnectionFactory
{
	protected String mqUsername = null;
	protected String mqPassword = null;
	
	public void setUsername(String mqUsername) {
		this.mqUsername = mqUsername;
	}
	public void setPassword(String mqPassword) {
		this.mqPassword = mqPassword;
	}

	public void afterPropertiesSet() {
		Assert.notNull(mqUsername, "'mqUsername' required");
		super.afterPropertiesSet();
	}
	
	/**
	 * this implementation make sure the connection creation is with user
	 * 
	 * @see super{@link #doCreateConnection()}
	 */
    protected Connection doCreateConnection() throws JMSException
    {
        ConnectionFactory cf = getTargetConnectionFactory();
        if((cf instanceof QueueConnectionFactory))
            return ((QueueConnectionFactory)cf).createQueueConnection(mqUsername, mqPassword);
        if((cf instanceof TopicConnectionFactory))
            return ((TopicConnectionFactory)cf).createTopicConnection(mqUsername, mqPassword);
        else
            return cf.createConnection(mqUsername, mqPassword);
    }

	@Override
	public Connection createConnection(String username, String password) throws JMSException {
		assertSameValue("username", mqUsername, username);
		assertSameValue("password", mqPassword, password);
		return super.createConnection();
	}

	@Override
	public QueueConnection createQueueConnection(String username, String password) throws JMSException {
		assertSameValue("username", mqUsername, username);
		assertSameValue("password", mqPassword, password);
		return super.createQueueConnection();
	}

	@Override
	public TopicConnection createTopicConnection(String username, String password) throws JMSException {
		assertSameValue("username", mqUsername, username);
		assertSameValue("password", mqPassword, password);
		return super.createTopicConnection();
	}

	/**
	 * 
	 * @param context
	 * @param expected
	 * @param actual
	 */
	protected void assertSameValue(String context, String expected, String actual) {
		if ( expected == actual ) return;
		if ( expected == null ) {
			if ( actual == null ) return;
		} else {
			if ( expected.equals(actual) ) return;
		}
		// if not returned so far, than throw
		throw new IllegalArgumentException(String.format("%s: '%s' does not match to expected '%s'", context, actual, expected));
	}

}
